package xuegao.study.mall.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.dto.rbac.UmsMemberRegisterDto;
import xuegao.study.mall.common.model.ums.UmsMember;
import xuegao.study.mall.web.feign.UmsMemberClient;
import xuegao.study.mall.web.service.UmsMemberService;

@RestController
@Api(tags = {"UmsMemberController"})
@Tag(name = "UmsMemberController", description = "会员登录注册管理")
@RequestMapping("/sso")
public class UmsMemberController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private UmsMemberClient umsMemberClient;

    @ApiOperation("获取验证码")
    @GetMapping(value = "/getAuthCode")
    public CommonResult getAuthCode(@RequestParam String telephone) {
        return umsMemberService.generateAuthCode(telephone);
    }

    /**
     * 会员注册
     * @return
     */
    @ApiOperation("会员注册")
    @PostMapping("/register")
    public CommonResult register(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String telephone,
                                 @RequestParam String authCode) {
        UmsMemberRegisterDto registerDto = new UmsMemberRegisterDto();
        registerDto.setUsername(username);
        registerDto.setPassword(password);
        registerDto.setTelephone(telephone);
        registerDto.setAuthCode(authCode);
        return umsMemberClient.register(registerDto);
    }

    /**
     * 会员登录
     * @return
     */
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public CommonResult login(@RequestParam String username,
                              @RequestParam String password) {
        return umsMemberService.login(username, password);
    }

    /**
     * 获取会员信息
     * @return
     */
    @ApiOperation("获取会员信息")
    @GetMapping("/info")
    public CommonResult info() {
        UmsMember member = umsMemberService.getCurrentMember();
        return CommonResult.success(member);
    }

}
